/*
   Splash Screen Plugin for Miranda-IM (www.miranda-im.org)
   (c) 2004-2007 nullbie, (c) 2005-2007 Thief

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   File name      : $URL: http://svn.mirandaim.ru/mainrepo/splashscreen/trunk/src/options.cpp $
   Revision       : $Rev: 599 $
   Last change on : $Date: 2007-03-19 23:15:38 +0300 (Mon, 19 Mar 2007) $
   Last change by : $Author: Thief $

   DESCRIPTION: Options dialog handling code

*/

#include "headers.h"

extern HINSTANCE hInst;
extern bool ShowSplash(bool preview);
extern bool png2dibavail;
extern char szIniFile [MAX_PATH], szSplashFile[MAX_PATH], szSoundFile[MAX_PATH];
extern char szPrefix[128];
static char szPath2Spash [MAX_PATH], szSoundFilePath[MAX_PATH];

// Reads values from ini file
void ReadIniConfig()
{
  static char inBuf[80];

  GetPrivateProfileString("Splash","Active","1",inBuf,sizeof(inBuf),szIniFile);
  options.active = atoi(inBuf);

  GetPrivateProfileString("Splash","PlaySound","0",inBuf,sizeof(inBuf),szIniFile);
  options.playsnd = atoi(inBuf);

  GetPrivateProfileString("Splash","FadeIn","1",inBuf,sizeof(inBuf),szIniFile);
  options.fadein = atoi(inBuf);

  GetPrivateProfileString("Splash","FadeOut","1",inBuf,sizeof(inBuf),szIniFile);
  options.fadeout = atoi(inBuf);
     
  GetPrivateProfileString("Splash","TimeToShow","0",inBuf,sizeof(inBuf),szIniFile);
  options.showtime = atoi(inBuf);

  GetPrivateProfileString("Splash","FadeinSpeed","1",inBuf,sizeof(inBuf),szIniFile);
  options.fisteps = atoi(inBuf);

  GetPrivateProfileString("Splash","FadeoutSpeed","1",inBuf,sizeof(inBuf),szIniFile);
  options.fosteps = atoi(inBuf);

  GetPrivateProfileString("Splash","InheritGlobalSound","1",inBuf,sizeof(inBuf),szIniFile);
  options.inheritGS = atoi(inBuf);

  GetPrivateProfileString("Splash","LoopSound","0",inBuf,sizeof(inBuf),szIniFile);
  options.loopsnd = atoi(inBuf);	

  GetPrivateProfileString("Splash","ShowVersion","0",inBuf,sizeof(inBuf),szIniFile);
  options.showversion = atoi(inBuf);

  GetPrivateProfileString("Splash","Random","0",inBuf,sizeof(inBuf),szIniFile);
  options.random = atoi(inBuf);
  
  GetPrivateProfileString("Splash","DisableAfterStartup","0",inBuf,sizeof(inBuf),szIniFile);
  options.runonce = atoi(inBuf);
}

BOOL CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	   switch (msg)
	   {
			case WM_INITDIALOG:
			{             
				TranslateDialogDefault(hwndDlg);
				if (!png2dibavail) 
				{
					ShowWindow(GetDlgItem(hwndDlg, IDC_PNG2DIBWARN), SW_SHOW);
					EnableWindow(GetDlgItem(hwndDlg, IDC_ACTIVE), false);
					EnableWindow(GetDlgItem(hwndDlg, IDC_RANDOM), false);
					EnableWindow(GetDlgItem(hwndDlg, IDC_SPLASHPATH), false);
					EnableWindow(GetDlgItem(hwndDlg, IDC_CHOOSESPLASH), false);
				}
				ReadIniConfig();
				char inBuf[80];
				GetPrivateProfileString("Splash","Path","splash\\splash.png",inBuf,sizeof(inBuf),szIniFile);
				SetWindowText(GetDlgItem(hwndDlg, IDC_SPLASHPATH),inBuf);
				GetPrivateProfileString("Splash","Sound","sounds\\startup.wav",inBuf,sizeof(inBuf),szIniFile);
				SetWindowText(GetDlgItem(hwndDlg, IDC_SNDPATH),inBuf);
				GetPrivateProfileString("Splash","VersionPrefix","",inBuf,sizeof(inBuf),szIniFile);
				SetWindowText(GetDlgItem(hwndDlg, IDC_VERSIONPREFIX),inBuf);
				if (options.active)	CheckDlgButton(hwndDlg, IDC_ACTIVE, BST_CHECKED);
				if (options.playsnd && !options.inheritGS) CheckDlgButton(hwndDlg, IDC_PLAYSND, BST_INDETERMINATE);
				else if (options.playsnd) CheckDlgButton(hwndDlg, IDC_PLAYSND, BST_CHECKED);
				//if (options.loopsnd) CheckDlgButton(hwndDlg, IDC_LOOPSOUND, BST_CHECKED);
				EnableWindow(GetDlgItem(hwndDlg, IDC_LOOPSOUND), false);
				if (options.fadein)	CheckDlgButton(hwndDlg, IDC_FADEIN, BST_CHECKED);
				if (options.fadeout) CheckDlgButton(hwndDlg, IDC_FADEOUT, BST_CHECKED);
				if (options.random)	CheckDlgButton(hwndDlg, IDC_RANDOM, BST_CHECKED);
				if (options.showversion) CheckDlgButton(hwndDlg, IDC_SHOWVERSION, BST_CHECKED);

				GetPrivateProfileString("Splash","TimeToShow","2000",inBuf,sizeof(inBuf),szIniFile);
				SetWindowText(GetDlgItem(hwndDlg, IDC_SHOWTIME),inBuf);
				GetPrivateProfileString("Splash","FadeinSpeed","5",inBuf,sizeof(inBuf),szIniFile);
				SetWindowText(GetDlgItem(hwndDlg, IDC_FISTEP),inBuf);
				GetPrivateProfileString("Splash","FadeoutSpeed","5",inBuf,sizeof(inBuf),szIniFile);
				SetWindowText(GetDlgItem(hwndDlg, IDC_FOSTEP),inBuf);

				SendDlgItemMessage(hwndDlg, IDC_SHOWTIME, EM_LIMITTEXT, 5, 0);
				SendDlgItemMessage(hwndDlg, IDC_ST_SPIN, UDM_SETRANGE32, 0, 20000);
				SendDlgItemMessage(hwndDlg, IDC_FI_SPIN, UDM_SETRANGE32, 1, 7);
				SendDlgItemMessage(hwndDlg, IDC_FO_SPIN, UDM_SETRANGE32, 1, 7);

				return TRUE;
			}
			
			case WM_COMMAND:
			{
				switch(LOWORD(wParam)) {
					case IDC_PREVIEW:
					{
						ShowSplash(true);
						break;
					}
					
					case IDC_ACTIVE:
					case IDC_PLAYSND:
					case IDC_LOOPSOUND:
					case IDC_FADEIN:
					case IDC_FADEOUT:
					case IDC_SHOWTIME:
					case IDC_RANDOM:
					case IDC_SHOWVERSION:
					case IDC_FISTEP:
					case IDC_FOSTEP:
					{
						if (IsDlgButtonChecked(hwndDlg, IDC_FADEIN))
						{
							EnableWindow(GetDlgItem(hwndDlg, IDC_FISTEP), true);
							EnableWindow(GetDlgItem(hwndDlg, IDC_FI_SPIN), true);
						}
						else
						{
							EnableWindow(GetDlgItem(hwndDlg, IDC_FISTEP), false);
							EnableWindow(GetDlgItem(hwndDlg, IDC_FI_SPIN), false);
						}
						if (IsDlgButtonChecked(hwndDlg, IDC_FADEOUT))
						{
							EnableWindow(GetDlgItem(hwndDlg, IDC_FOSTEP), true);
							EnableWindow(GetDlgItem(hwndDlg, IDC_FO_SPIN), true);
						}
						else
						{
							EnableWindow(GetDlgItem(hwndDlg, IDC_FOSTEP), false);
							EnableWindow(GetDlgItem(hwndDlg, IDC_FO_SPIN), false);
						}
						  
						if ((HWND)lParam != GetFocus())
                        return 0;
						else {
							SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
							break;
						}
						break;
					}

					case IDC_CHOOSESPLASH:
					{
						static char szTempPath[MAX_PATH], initDir[MAX_PATH];
						char *pos;
						
						lstrcpy(initDir,szSplashFile);
						pos = strrchr(initDir, '\\');
						if(pos != NULL) *pos = 0;
						
						OPENFILENAMEA ofn = {0};
						ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
						ofn.lpstrFilter = "PNG and BMP files\0*.png;*.bmp\0\0";
						ofn.hwndOwner = 0;
						ofn.lpstrFile = szTempPath;
						ofn.nMaxFile = MAX_PATH;
						ofn.nMaxFileTitle = MAX_PATH;
						ofn.Flags = OFN_HIDEREADONLY;
						ofn.lpstrInitialDir = initDir;
						*szTempPath = '\0';
						ofn.lpstrDefExt = "";

						if (GetOpenFileNameA(&ofn)) {
							lstrcpy(szSplashFile,szTempPath);
							
							#ifdef _DEBUG
								logMessage("Set path", szSplashFile);
							#endif
				
							// Make path relative
							int result = CallService(MS_UTILS_PATHTORELATIVE, (WPARAM)szTempPath, (LPARAM)szPath2Spash);			
							
							if(result && lstrlenA(szPath2Spash) > 0) {

								if (options.random)
								{
									char *pos;
									pos = strrchr(szPath2Spash, '\\');
									if (pos != NULL) 
									{ 
										*pos = 0;
										lstrcat(szPath2Spash,"\\");
									}
								}

								SetWindowText(GetDlgItem(hwndDlg, IDC_SPLASHPATH),szPath2Spash);
							}
													
							SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
						}
					
						break;
					}

					case IDC_CHOOSESND:
					{
						static char szTempPath[MAX_PATH], initDir[MAX_PATH];
						char *pos;
						
						lstrcpy(initDir,szSoundFile);
						pos = strrchr(initDir, '\\');
						if(pos != NULL) *pos = 0;
						
						OPENFILENAMEA ofn = {0};
						ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
						ofn.lpstrFilter = "Sound files\0*.wav\0\0";
						ofn.hwndOwner = 0;
						ofn.lpstrFile = szTempPath;
						ofn.nMaxFile = MAX_PATH;
						ofn.nMaxFileTitle = MAX_PATH;
						ofn.Flags = OFN_HIDEREADONLY;
						ofn.lpstrInitialDir = initDir;
						*szTempPath = '\0';
						ofn.lpstrDefExt = "";

						if (GetOpenFileNameA(&ofn)) {
							lstrcpy(szSoundFile,szTempPath);

							#ifdef _DEBUG
								logMessage("Set sound path", szSoundFile);
							#endif
				
							// Make path relative
							int result = CallService(MS_UTILS_PATHTORELATIVE, (WPARAM)szTempPath, (LPARAM)szSoundFilePath);			
							
							if(result && lstrlenA(szSoundFile) > 0) {
								SetWindowText(GetDlgItem(hwndDlg, IDC_SNDPATH),szSoundFilePath);
							}
													
							SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
						}
					
						break;
					}
				}
				
				default:
					if (HIWORD(wParam) != EN_CHANGE || (HWND) lParam != GetFocus())
						return 0;
					else
						SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
				
				break;
			}

			case WM_NOTIFY:
			{
				if (((LPNMHDR)lParam)->idFrom == 0)
					switch (((LPNMHDR)lParam)->code)
					{
						 case PSN_APPLY:
							 {
								static char tmp[MAX_PATH];

								GetWindowText(GetDlgItem(hwndDlg, IDC_SPLASHPATH),tmp,MAX_PATH);
								WritePrivateProfileString("Splash","Path",tmp,szIniFile);
								lstrcpy(szSplashFile,tmp);

								GetWindowText(GetDlgItem(hwndDlg, IDC_SNDPATH),tmp,MAX_PATH);
								WritePrivateProfileString("Splash","Sound",tmp,szIniFile);
								lstrcpy(szSoundFile,tmp);

								GetWindowText(GetDlgItem(hwndDlg, IDC_VERSIONPREFIX),tmp,MAX_PATH);
								static char tmpstr[128] = {0};
								strcat(tmpstr,"\"");
								strcat(tmpstr,tmp);
								strcat(tmpstr,"\"");
								WritePrivateProfileString("Splash","VersionPrefix",tmpstr,szIniFile);
								ZeroMemory(&tmpstr,sizeof(tmpstr));
								strcpy(szPrefix,tmp);

								GetWindowText(GetDlgItem(hwndDlg, IDC_SHOWTIME),tmp,MAX_PATH);
								WritePrivateProfileString("Splash","TimeToShow",tmp,szIniFile);

								GetWindowText(GetDlgItem(hwndDlg, IDC_FISTEP),tmp,MAX_PATH);
								WritePrivateProfileString("Splash","FadeinSpeed",tmp,szIniFile);
								options.fisteps = atoi(tmp);

								GetWindowText(GetDlgItem(hwndDlg, IDC_FOSTEP),tmp,MAX_PATH);
								WritePrivateProfileString("Splash","FadeoutSpeed",tmp,szIniFile);
								options.fosteps = atoi(tmp);

								if (IsDlgButtonChecked(hwndDlg, IDC_ACTIVE))
									WritePrivateProfileString("Splash","Active","1",szIniFile);
								else
									WritePrivateProfileString("Splash","Active","0",szIniFile);

								if (IsDlgButtonChecked(hwndDlg, IDC_PLAYSND))
								{
									WritePrivateProfileString("Splash","PlaySound","1",szIniFile);
									WritePrivateProfileString("Splash","InheritGlobalSound","1",szIniFile);
									options.playsnd = 1;
									options.inheritGS = 1;
								}
								else
								{
									WritePrivateProfileString("Splash","PlaySound","0",szIniFile);
									options.playsnd = 0;
									WritePrivateProfileString("Splash","InheritGlobalSound","0",szIniFile);
									options.inheritGS = 0;
								}
								
								if (IsDlgButtonChecked(hwndDlg, IDC_PLAYSND) == BST_INDETERMINATE)
								{
									WritePrivateProfileString("Splash","PlaySound","1",szIniFile);
									options.playsnd = 1;
									WritePrivateProfileString("Splash","InheritGlobalSound","0",szIniFile);
									options.inheritGS = 0;
								}
								
								if (IsDlgButtonChecked(hwndDlg, IDC_LOOPSOUND))
								{
									WritePrivateProfileString("Splash","LoopSound","1",szIniFile);
									options.loopsnd = 1;
								}
								else
								{
									WritePrivateProfileString("Splash","LoopSound","0",szIniFile);
									options.loopsnd = 0;
								}

								if (IsDlgButtonChecked(hwndDlg, IDC_FADEIN))
								{
									WritePrivateProfileString("Splash","FadeIn","1",szIniFile);
									options.fadein = 1;
								}
								else
								{
									WritePrivateProfileString("Splash","FadeIn","0",szIniFile);
									options.fadein = 0;
								}
								if (IsDlgButtonChecked(hwndDlg, IDC_FADEOUT))
								{
									WritePrivateProfileString("Splash","FadeOut","1",szIniFile);
									options.fadeout = 1;
								}
								else
								{
									WritePrivateProfileString("Splash","FadeOut","0",szIniFile);
									options.fadeout = 0;
								}
								if (IsDlgButtonChecked(hwndDlg, IDC_RANDOM))
								{
									WritePrivateProfileString("Splash","Random","1",szIniFile);
									options.random = 1;
								}
								else
								{
									WritePrivateProfileString("Splash","Random","0",szIniFile);
									options.random = 0;
								}
								if (IsDlgButtonChecked(hwndDlg, IDC_SHOWVERSION))
								{
									WritePrivateProfileString("Splash","ShowVersion","1",szIniFile);
									options.showversion = 1;
								}
								else
								{
									WritePrivateProfileString("Splash","ShowVersion","0",szIniFile);
									options.showversion = 0;
								}
								return TRUE;
							 }
					}
			}
			
			case WM_DESTROY:
            break;
	   }
	return FALSE;
}

int OptInit(WPARAM wParam, LPARAM lParam)
{
    OPTIONSDIALOGPAGE odp;
	
	ZeroMemory(&odp, sizeof(odp));
	odp.cbSize = sizeof(odp);
	odp.position = 0;
	odp.hInstance = hInst;
	odp.pszGroup = Translate("Customize");
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_SPLASH_OPT);
	odp.pszTitle = Translate("Splash Screen");
	odp.pfnDlgProc = DlgProcOptions;
	odp.flags = ODPF_BOLDGROUPS;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM) &odp);
	return 0;
}